<!DOCTYPE html>
<html>
<head>
    <title>管理</title>
    <#include "/header.html">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div style="margin: 10px 0">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="搜索用户名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-primary layui-border-blue" lay-submit lay-filter="form-search-btn"><i class="layui-icon layui-icon-search"></i>搜索</button>
                        <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="form-reset-btn"><i class="layui-icon layui-icon-refresh"></i>重置</button>
                    </div>
                </div>
            </form>
        </div>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <#if shiro.hasPermission("app:forum:save")>
        <button class="layui-btn layui-btn-normal" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加 </button>
        </#if>
    </div>
</script>


<script type="text/html" id="currentTableStatusTpl">
    {{# if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">启用</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-grey">禁用</span>
    {{# } }}
</script>

<script type="text/html" id="currentTableAnonymousTpl">
    {{# if(d.status == 1){ }}
    <span class="layui-badge layui-bg-green">匿名</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-grey">不匿名</span>
    {{# } }}
</script>

<script type="text/html" id="currentTableBar">
    <#if shiro.hasPermission("app:forum:save")>
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    </#if>
    <#if shiro.hasPermission("app:forum:delete")>
    <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </#if>
</script>

<script id="positionListTpl" type="text/html">
    <select name="position">
        <option value="">选择位置</option>
        {{#  layui.each(d, function(index, item){ }}
        <option value="{{item.value}}">{{item.name}}</option>
        {{#  }); }}
    </select>
</script>
<script type="text/javascript">
    var formDataId = 0;
    var positionList = [];
    layui.use(['form', 'table', 'laytpl'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laytpl = layui.laytpl;
        tableSearch({},'forum/list');
        function tableSearch(data,url) {
        	table.render({
                elem: '#currentTableId',
			    where:data,
                url: serverUrl(url),
                toolbar: '#toolbarDemo'

                    ,layEvent: 'LAYTABLE_TIPS',
                defaultToolbar: ['filter'],
                cols: [[
                    {type: "numbers", width: 80},
                    {field: 'username', minWidth: 150, title: '用户名'},
                    {field: 'avatar', title: '头像', minWidth: 150, templet: function (d) {return '<img src="'+d.avatar+'" alt="">';}},
                    {field: 'title', title: '标题', width: 120, templet: '#currentTableTypeTpl'},
                    {field: 'content', title: '主帖内容', minWidth: 100},
                    {field: 'anonymous', title: '是否匿名', minWidth: 80, templet: '#currentTableAnonymousTpl'},
                    {field: 'images', title: '图片', minWidth: 150},
                    {field: 'agree', title: '点赞人数', minWidth: 70},
                    {field: 'visitors', title: '浏览人数', minWidth: 70},
                    {field: 'comment', title: '总评论数量', minWidth: 80},
                    {field: 'createTime', title: '创建时间', minWidth: 80},
                    {field: 'status', title: '状态', width: 80, templet: '#currentTableStatusTpl'},
                    {title: '操作', width: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
    		 	 done: function (res, curr, count){
    			  $('table').css('width', '100%');
    			  //这里设置表格的宽度为100%
    			  },
                limits: [15, 30, 50],
                limit: 15,
                page: true
            });
        };
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                formDataId = 0;
                openFormView('app/forum/form.html', '添加');

            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                formDataId = data.id;
                openFormView('app/forum/form.html', '编辑');
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除吗？', function (index) {
                    deleteTableRow('app/forum/delete', [data.id], function (rs) {
                        obj.del();
                    });
                    layer.close(index);
                });
            }
        });

        // 监听搜索操作
        form.on('submit(form-search-btn)', function (data) {
            //执行搜索重载
            if (data.field.username) {
            	tableSearch({"username":data.field.username},'forum/search/username');
            //table.filterData(data.field.title);
	        } else {
	            tableSearch({},'forum/list');
	            //table.clearFilter();
	        }
	        return false;
	    });
        form.on('submit(form-reset-btn)', function (data) {
            tableSearch({},'forum/list');
            $("#searchForm")[0].reset();
            //table.clearFilter();
            return false;
        });

    });
</script>

</body>
</html>